package com.atguigu.yygh.hosp.service.impl;


import com.alibaba.fastjson.JSONObject;

import com.atguigu.yygh.cmn.DcitFeignClient;
import com.atguigu.yygh.hosp.Repository.HospitalRepository;
import com.atguigu.yygh.hosp.mapper.HospitalMapper;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class HospitalServiceImpl  extends ServiceImpl<HospitalMapper, Hospital> implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DcitFeignClient dcitFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap),Hospital.class);
        //判断是否存在
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if(null != targetHospital) {
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
//0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }



    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        return  hospitalByHoscode;
    }

//    实现分页查询
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort=Sort.by(Sort.Direction.DESC,"createTime");
        Pageable pageable= PageRequest.of(page-1,limit,sort);
        ExampleMatcher exampleMatcher=ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        Example<Hospital> example=Example.of(hospital,exampleMatcher);
        Page<Hospital> all = hospitalRepository.findAll(example, pageable);
        all.getContent().stream().forEach(item->{
//            设置hostype
            this.setHospitalType(item);
        });
        return  all;
    }

    @Override
    public void updateStatus(String id,Integer status) {
        System.out.println("开始执行:"+id);
        Hospital hospital = hospitalRepository.findById(id).get();
        System.out.println(hospital.getHosname());
        if (hospital != null) {
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String, Object> getHospById(String id) {
        Hospital hospital = this.setHospitalType(hospitalRepository.findById(id).get());
        Map<String, Object> map=new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return  map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return  "没有名字";
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> list = hospitalRepository.findByHosname(hosname);
        return list;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dcitFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dcitFeignClient.getName(hospital.getProvinceCode());
        String cityString = dcitFeignClient.getName(hospital.getCityCode());
        String districtString = dcitFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }

    private Hospital setHospitalType(Hospital hospital) {
        String HostypeString = dcitFeignClient.getName("Hostype", hospital.getHostype());
//        查询省市
        String provinceString = dcitFeignClient.getName(hospital.getProvinceCode());
        String cityString = dcitFeignClient.getName(hospital.getCityCode());
        String districtString = dcitFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("HostypeString",HostypeString);
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        return hospital;
    }
//    为分页查询的数据进行增强属性处理

}
